package com.liwen.service.coop.jd;

import com.alibaba.fastjson.JSON;
import com.liwen.api.enums.Ins;
import com.liwen.api.vo.OrderVO;
import com.liwen.dao.entity.OrderEntity;
import com.liwen.service.coop.BaseBizService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;

import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

@Service("jDService")
public class JDService extends BaseBizService<JDClient> {
    private Logger log = LoggerFactory.getLogger(this.getClass());

//    @Resource
//    private OrderSend orderSend;

    @Override
    protected JDClient buildClient() {
        return new JDClient();
    }

    @Override
    public Ins ins() {
        return Ins.JD;
    }

    public void testJD() {//开线程支持异步
        System.out.println("aa");
        run(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("cc");
                } catch (Exception e) {

                }
            }
        });
        System.out.println("bb");
    }

    public OrderVO getOrderInfo(String sn) {
        OrderVO vo = null;
        String key = "order_test_sfdsfsdfsdfsdf";
        ShardedJedis jedis = redis();
        try {
            String json = redis().get(key);
            if (StringUtils.isNotBlank(json)) {
                return JSON.parseObject(json, OrderVO.class);
            }
            OrderEntity entity = order().bysn(ins().getCode(), sn);
            if (entity != null) {
                vo = client().changeOrder(entity);
                jedis.setex(key, 60 * 2, JSON.toJSONString(vo));
            }
        } catch (Exception e) {
            log.error("getOrderInfo=err:{}", e.getMessage());
        } finally {
            jedis.close();
        }
        return vo;
    }

    public void test() {
        ShardedJedis jedis = redis();
        long time = System.currentTimeMillis();
        String key = "testaa";
        Set<String> set = jedis.zrangeByScore(key, 0, time);
        jedis.zremrangeByScore(key, 0, time);
        //jedis.zrem(key, set.toArray(new String[0]));
        jedis.close();
    }

    public void testCountDown() {
        final CountDownLatch latch = new CountDownLatch(3);
        final AtomicLong amount = new AtomicLong(0);
        for (int i = 0; i < 3; i++) {
            run(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        amount.incrementAndGet();
                        System.out.println("cc");
                    } catch (Exception e) {
                    } finally {
                        latch.countDown();
                    }
                }
            });
        }
        try {
            latch.await(3, TimeUnit.MINUTES); //设置3分钟的工作时间
            if (latch.getCount() > 0) {
                log.error("countDownLatch-thread-outTime");
                return;
            }
        } catch (InterruptedException e) {
            log.error("countDownLatch-interruption-error", e);
        }
        System.out.println(amount);
    }

    public void testMessage() {
//        orderSend.testSendMsg();
    }

}
